﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class Projects : PageBaseClass
{
    ProjectFactory pFactory;
    protected void Page_Load(object sender, EventArgs e)
    {
        pFactory = new ProjectFactory();
        List<Project> projects = pFactory.GetAllBy(Project.ProjectFields.Office_Code, _author.office_Code);
        this.gvProjects.DataSource = projects;
        this.gvProjects.DataBind();

        if (!IsPostBack)
        {
            DepartmentFactory dFactory = new DepartmentFactory();
            List<Department> deptList = dFactory.GetAllBy(Department.DepartmentFields.Office_Code, _author.office_Code);

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = Department.DepartmentFields.Department_Name.ToString();
            ddlDept.DataValueField = Department.DepartmentFields.Department_Code.ToString();
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }
        }
    }

    private void BindTheUsers(int deptCode)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAllBy(UserInfo.UserInfoFields.Department_Code, ddlDept.SelectedValue);
        }
        else
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAll();
        }

        this.ddlUsers.DataTextField = UserInfo.UserInfoFields.LastName.ToString();// +", " + UserInfo.UserInfoFields.LastName.ToString();
        this.ddlUsers.DataTextFormatString = "Ms {0}";
        this.ddlUsers.DataValueField = UserInfo.UserInfoFields.User_Code.ToString();
        this.ddlUsers.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Project Proj = new Project();
        Proj.Project_Title = txtProjectTitle.Text;
        Proj.Project_Desc = txtDescription.Text;
        Proj.Open_Date = DateTime.Parse(txtStartDate.Text);
        Proj.Assigned_To = int.Parse(ddlUsers.SelectedValue);
        Proj.DeadLine = DateTime.Parse(txtDeadLine.Text);

        Proj.Open_By = _author.user_Code;
        Proj.Office_Code = _author.office_Code;

        pFactory = new ProjectFactory();
        if (pFactory.Insert(Proj))
        {
            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }
}
